{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "31d132f3",
      "metadata": {},
      "source": [
        "# Python 类型转换详解\n",
        "\n",
        "在Python中，类型转换是将数据从一种类型转换为另一种类型的过程，主要分为隐式类型转换和显式类型转换两种方式。以下是详细说明：\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "b101b761",
      "metadata": {},
      "source": [
        "## 一、隐式类型转换\n",
        "\n",
        "隐式类型转换由Python自动完成，通常发生在不同数值类型（如整数、浮点数）运算时，Python会将低精度类型转换为高精度类型以避免数据丢失。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "8af33364",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "c的类型：<class 'float'>，值：13.14\n"
          ]
        }
      ],
      "source": [
        "a = 10\n",
        "b = 3.14\n",
        "c = a + b\n",
        "print(f'c的类型：{type(c)}，值：{c}')"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "809d6b9a",
      "metadata": {},
      "source": [
        "## 二、显式类型转换\n",
        "\n",
        "显式类型转换（强制转换）通过Python内置函数手动实现，用于明确将数据转换为目标类型。以下是常见转换函数：\n",
        "\n",
        "### 1. int()：转换为整数\n",
        "- 浮点数：直接截断小数部分（非四舍五入）\n",
        "- 字符串：仅当字符串内容为纯数字时可转换"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "094b849d",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "浮点数9.9转整数：9\n",
            "字符串123转整数：123\n"
          ]
        }
      ],
      "source": [
        "# 浮点数转整数\n",
        "f_num = 9.9\n",
        "int_f = int(f_num)\n",
        "print(f'浮点数{ f_num }转整数：{ int_f }')\n",
        "\n",
        "# 字符串转整数（有效）\n",
        "str_num = '123'\n",
        "int_str = int(str_num)\n",
        "print(f'字符串{ str_num }转整数：{ int_str }')"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "10dc4882",
      "metadata": {},
      "source": [
        "### 2. float()：转换为浮点数\n",
        "- 整数：末尾添加.0\n",
        "- 字符串：支持纯数字或带小数点的数字字符串"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "a3a0d8e6",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "整数5转浮点数：5.0\n",
            "字符串3.14转浮点数：3.14\n"
          ]
        }
      ],
      "source": [
        "# 整数转浮点数\n",
        "i_num = 5\n",
        "float_i = float(i_num)\n",
        "print(f'整数{ i_num }转浮点数：{ float_i }')\n",
        "\n",
        "# 字符串转浮点数\n",
        "str_float = '3.14'\n",
        "float_str = float(str_float)\n",
        "print(f'字符串{ str_float }转浮点数：{ float_str }')"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "270f9ce4",
      "metadata": {},
      "source": [
        "### 3. str()：转换为字符串\n",
        "- 任意类型数据均可转换为对应字符串表示"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bc252574",
      "metadata": {},
      "outputs": [],
      "source": [
        "num = 100\n",
        "str_num = str(num)\n",
        "print(f'整数{ num }转字符串：{ str_num }，类型：{ type(str_num) }')\n",
        "\n",
        "f = 3.14\n",
        "str_f = str(f)\n",
        "print(f'浮点数{ f }转字符串：{ str_f }，类型：{ type(str_f) }')"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "dc2eca6e",
      "metadata": {},
      "source": [
        "## 三、注意事项\n",
        "1. 非数值字符串转换为数值类型时会报错（如'abc'转int）。\n",
        "2. 布尔值（True/False）可转换为整数（1/0）或浮点数（1.0/0.0）。\n",
        "3. 类型转换不会修改原始数据，而是生成新类型的副本。\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "ml",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
